package oer.halk.intercept_demo.utils;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**
 * @Author halk
 * @Description 对信息进行签名
 * @Date 2022/1/26 14:46
 **/
@SpringBootApplication
public class SignUtil {

    public static void main(String[] args) throws IOException {

        String info = "我是信息111111111111111111111111111111111111111111111111111111111";
        byte[] infoBytes = info.getBytes();
        //获取签名器 可加入公钥 秘钥
        Sign sign = SecureUtil.sign(SignAlgorithm.MD2withRSA);
        //签名后信息
        byte[] signByte = sign.sign(infoBytes);
        //返回签名
        String signInfo2Web = new BASE64Encoder().encode(signByte);
        System.out.println(signInfo2Web);

        //签名校验
        byte[] signInfo4Web = new BASE64Decoder().decodeBuffer(signInfo2Web);
        System.out.println(sign.verify(infoBytes, signInfo4Web));
    }

}
